package com.cloudsigns.factorymanager2.produce.activity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditProcessAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.iview.IAddProductWorkPartView;
import com.cloudsigns.factorymanager2.produce.iview.IGetProductWorkPartListView;
import com.cloudsigns.factorymanager2.produce.presenter.AddProductWorkPartPresenter;
import com.cloudsigns.factorymanager2.produce.presenter.GetProductWorkPartListPresenter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

public class ProduceEditWorkpartActivity extends BaseActivity implements IGetProductWorkPartListView, IAddProductWorkPartView, ItemRemoveListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private ImageView img_add;
    private ListView lv;

    private ProduceEditProcessAdapter adapter;
    private List<String> dataList = new ArrayList<>();
    private GetProductWorkPartListPresenter getProductWorkPartListPresenter;

    private AddProductWorkPartPresenter addProductWorkPartPresenter;
    private EditText edit_content;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_process);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        img_add = findViewById(R.id.img_add);
        lv = findViewById(R.id.lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_process));

        getProductWorkPartListPresenter = new GetProductWorkPartListPresenter(this);
        getProductWorkPartListPresenter.getProductWorkPartList();

        adapter = new ProduceEditProcessAdapter(this, TAG);
        adapter.setRemoveListener(this);
        adapter.setData(dataList);
        lv.setAdapter(adapter);

        addProductWorkPartPresenter = new AddProductWorkPartPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_add.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 添加工序
            case R.id.img_add:
                LayoutInflater inflater = getLayoutInflater();
                View view1 = inflater.inflate(R.layout.dialog_edit, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                edit_content = view1.findViewById(R.id.edit_content);
                final Dialog dialog = new Dialog(this);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);

                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if (!TextUtils.isEmpty(edit_content.getText().toString())) {
                            addProductWorkPartPresenter.addProductWorkPart();
                            dialog.dismiss();
                        } else {
                            showToast(getResources().getString(R.string.input_content_not_empty));
                        }
                    }
                });
                break;
        }
    }

    @Override
    public void getDataSuccessed(List<WorkPartBean> workPartBeanList) {
        if (null != workPartBeanList && workPartBeanList.size() > 0) {
            dataList.clear();
            for (WorkPartBean wp : workPartBeanList) {
                dataList.add(wp.getWorkPartName());
            }

            adapter.setData(dataList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public String getName() {
        return edit_content.getText().toString();
    }

    @Override
    public void addProductWorkPartSuccessed() {

        dataList.add(getName());
        adapter.setData(dataList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void addProductWorkPartFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void remove(int positon) {
        if (positon >= 0 && positon < dataList.size()) {
            dataList.remove(positon);
            adapter.setData(dataList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }
}
